import { Request, Response } from "express";
import * as DepartmentService from "../services/department.service";
import { ResponseUtil } from "../utils/response.util";
// 获取所有部门
export const getAllDepartment = async (req: Request, res: Response) => {
  try {
    const page = Number(req.query.page);
    const pageSize = Number(req.query.pageSize);
    const options = req.body;
    const departments = await DepartmentService.getAllDepartments(
      page,
      pageSize,
      options
    );
    ResponseUtil.success(res, departments);
  } catch (err) {
    console.log(err);
    ResponseUtil.error(res, err, "获取部门失败");
  }
};

// 获取部门角色详情
export const getDepartmentById = async (req: Request, res: Response) => {
  try {
    const id = Number(req.params.id);
    const department = await DepartmentService.getDepartmentById(id);
    if (!department) return ResponseUtil.error(res, null, "部门不存在");
    return ResponseUtil.success(res, department);
  } catch (err) {
    ResponseUtil.error(res, err, "获取部门角色失败");
  }
};

// 创建部门
export const createDepartment = async (req: Request, res: Response) => {
  try {
    const department = await DepartmentService.createDepartment(req.body);
    ResponseUtil.success(res, department);
  } catch (err) {
    ResponseUtil.error(res, err, "创建部门失败");
  }
};

// 更新部门
export const updateDepartment = async (req: Request, res: Response) => {
  try {
    const id = Number(req.params.id);
    const department = await DepartmentService.updateDepartment(id, req.body);
    ResponseUtil.success(res, department);
  } catch (err) {
    ResponseUtil.error(res, err, "更新部门失败");
  }
};

// 删除部门
export const deleteDepartment = async (req: Request, res: Response) => {
  try {
    const id = Number(req.params.id);
    const deleteOne = await DepartmentService.deleteDepartment(id);
    ResponseUtil.success(res, deleteOne, "删除成功");
  } catch (err) {
    ResponseUtil.error(res, err, "删除部门失败");
  }
};
